home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_01 / allison / atox2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-02  |  488 b   |  28 lines

  1. LISTING 2 - A portable version of Listing 1
  2.  
  3. #include <ctype.h>
  4. #include <assert.h>
  5. #include <string.h>
  6.  
  7. long atox(char *s)
  8. {
  9.      char xdigs[] = "0123456789ABCDEF";
  10.      long sum;
  11.  
  12.      assert(s);
  13.  
  14.      /* Skip whitespace */
  15.      while (isspace(*s))
  16.           ++s;
  17.  
  18.      /* Do the conversion */
  19.      for (sum = 0L; isxdigit(*s); ++s)
  20.      {
  21.           int digit = strchr(xdigs,toupper(*s)) - xdigs;
  22.           sum = sum*16L + digit;
  23.      }
  24.  
  25.      return sum;
  26. }
  27.  
  28.